home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / number1a / decbin.frm (.txt) next >
Encoding:
Visual Basic Form  |  1999-07-29  |  16.6 KB  |  567 lines

  1. VERSION 5.00
  2. Begin VB.Form frmDecBin 
  3.    Caption         =   "Number Base Convertor - Decimal to Binary"
  4.    ClientHeight    =   3420
  5.    ClientLeft      =   60
  6.    ClientTop       =   630
  7.    ClientWidth     =   6855
  8.    LinkTopic       =   "Form1"
  9.    Picture         =   "DecBin.frx":0000
  10.    ScaleHeight     =   3420
  11.    ScaleWidth      =   6855
  12.    StartUpPosition =   2  'CenterScreen
  13.    Begin VB.TextBox txthex2 
  14.       Height          =   285
  15.       Left            =   2040
  16.       TabIndex        =   15
  17.       Top             =   3120
  18.       Visible         =   0   'False
  19.       Width           =   375
  20.    End
  21.    Begin VB.TextBox txthex1 
  22.       Height          =   285
  23.       Left            =   1320
  24.       TabIndex        =   14
  25.       Top             =   3120
  26.       Visible         =   0   'False
  27.       Width           =   375
  28.    End
  29.    Begin VB.TextBox txtnum 
  30.       Height          =   285
  31.       Left            =   240
  32.       TabIndex        =   13
  33.       Top             =   3000
  34.       Visible         =   0   'False
  35.       Width           =   735
  36.    End
  37.    Begin VB.CommandButton Command2 
  38.       Caption         =   "Convert"
  39.       BeginProperty Font 
  40.          Name            =   "Times New Roman"
  41.          Size            =   9.75
  42.          Charset         =   0
  43.          Weight          =   700
  44.          Underline       =   0   'False
  45.          Italic          =   0   'False
  46.          Strikethrough   =   0   'False
  47.       EndProperty
  48.       Height          =   375
  49.       Left            =   3720
  50.       TabIndex        =   3
  51.       Top             =   2640
  52.       Width           =   1215
  53.    End
  54.    Begin VB.TextBox txtDec 
  55.       BeginProperty Font 
  56.          Name            =   "Tahoma"
  57.          Size            =   9.75
  58.          Charset         =   0
  59.          Weight          =   400
  60.          Underline       =   0   'False
  61.          Italic          =   0   'False
  62.          Strikethrough   =   0   'False
  63.       EndProperty
  64.       Height          =   405
  65.       Left            =   2640
  66.       TabIndex        =   1
  67.       Top             =   2640
  68.       Width           =   495
  69.    End
  70.    Begin VB.CommandButton Command1 
  71.       Caption         =   "Exit"
  72.       BeginProperty Font 
  73.          Name            =   "Times New Roman"
  74.          Size            =   9.75
  75.          Charset         =   0
  76.          Weight          =   700
  77.          Underline       =   0   'False
  78.          Italic          =   0   'False
  79.          Strikethrough   =   0   'False
  80.       EndProperty
  81.       Height          =   375
  82.       Left            =   5400
  83.       TabIndex        =   0
  84.       Top             =   2640
  85.       Width           =   855
  86.    End
  87.    Begin VB.Label lblhex 
  88.       BackStyle       =   0  'Transparent
  89.       BeginProperty Font 
  90.          Name            =   "Tahoma"
  91.          Size            =   9.75
  92.          Charset         =   0
  93.          Weight          =   700
  94.          Underline       =   0   'False
  95.          Italic          =   0   'False
  96.          Strikethrough   =   0   'False
  97.       EndProperty
  98.       ForeColor       =   &H000000FF&
  99.       Height          =   255
  100.       Left            =   360
  101.       TabIndex        =   16
  102.       Top             =   2160
  103.       Width           =   6255
  104.    End
  105.    Begin VB.Label lblout 
  106.       BackStyle       =   0  'Transparent
  107.       BeginProperty Font 
  108.          Name            =   "Tahoma"
  109.          Size            =   9.75
  110.          Charset         =   0
  111.          Weight          =   700
  112.          Underline       =   0   'False
  113.          Italic          =   0   'False
  114.          Strikethrough   =   0   'False
  115.       EndProperty
  116.       ForeColor       =   &H000000FF&
  117.       Height          =   255
  118.       Left            =   360
  119.       TabIndex        =   12
  120.       Top             =   1800
  121.       Width           =   6255
  122.    End
  123.    Begin VB.Image bitty 
  124.       Height          =   480
  125.       Index           =   7
  126.       Left            =   240
  127.       Picture         =   "DecBin.frx":4B146
  128.       Top             =   960
  129.       Width           =   480
  130.    End
  131.    Begin VB.Image bitty 
  132.       Height          =   480
  133.       Index           =   6
  134.       Left            =   1080
  135.       Picture         =   "DecBin.frx":4B588
  136.       Top             =   960
  137.       Width           =   480
  138.    End
  139.    Begin VB.Image bitty 
  140.       Height          =   480
  141.       Index           =   5
  142.       Left            =   1920
  143.       Picture         =   "DecBin.frx":4B9CA
  144.       Top             =   960
  145.       Width           =   480
  146.    End
  147.    Begin VB.Image bitty 
  148.       Height          =   480
  149.       Index           =   4
  150.       Left            =   2760
  151.       Picture         =   "DecBin.frx":4BE0C
  152.       Top             =   960
  153.       Width           =   480
  154.    End
  155.    Begin VB.Image bitty 
  156.       Height          =   480
  157.       Index           =   3
  158.       Left            =   3600
  159.       Picture         =   "DecBin.frx":4C24E
  160.       Top             =   960
  161.       Width           =   480
  162.    End
  163.    Begin VB.Image bitty 
  164.       Height          =   480
  165.       Index           =   2
  166.       Left            =   4440
  167.       Picture         =   "DecBin.frx":4C690
  168.       Top             =   960
  169.       Width           =   480
  170.    End
  171.    Begin VB.Image bitty 
  172.       Height          =   480
  173.       Index           =   1
  174.       Left            =   5280
  175.       Picture         =   "DecBin.frx":4CAD2
  176.       Top             =   960
  177.       Width           =   480
  178.    End
  179.    Begin VB.Image bitty 
  180.       Height          =   480
  181.       Index           =   0
  182.       Left            =   6120
  183.       Picture         =   "DecBin.frx":4CF14
  184.       Top             =   960
  185.       Width           =   480
  186.    End
  187.    Begin VB.Label Label2 
  188.       BackStyle       =   0  'Transparent
  189.       Caption         =   "1"
  190.       BeginProperty Font 
  191.          Name            =   "Tahoma"
  192.          Size            =   9.75
  193.          Charset         =   0
  194.          Weight          =   700
  195.          Underline       =   0   'False
  196.          Italic          =   0   'False
  197.          Strikethrough   =   0   'False
  198.       EndProperty
  199.       ForeColor       =   &H000000FF&
  200.       Height          =   255
  201.       Index           =   7
  202.       Left            =   6240
  203.       TabIndex        =   11
  204.       Top             =   600
  205.       Width           =   375
  206.    End
  207.    Begin VB.Label Label2 
  208.       BackStyle       =   0  'Transparent
  209.       Caption         =   "2"
  210.       BeginProperty Font 
  211.          Name            =   "Tahoma"
  212.          Size            =   9.75
  213.          Charset         =   0
  214.          Weight          =   700
  215.          Underline       =   0   'False
  216.          Italic          =   0   'False
  217.          Strikethrough   =   0   'False
  218.       EndProperty
  219.       ForeColor       =   &H000000FF&
  220.       Height          =   255
  221.       Index           =   6
  222.       Left            =   5400
  223.       TabIndex        =   10
  224.       Top             =   600
  225.       Width           =   375
  226.    End
  227.    Begin VB.Label Label2 
  228.       BackStyle       =   0  'Transparent
  229.       Caption         =   "4"
  230.       BeginProperty Font 
  231.          Name            =   "Tahoma"
  232.          Size            =   9.75
  233.          Charset         =   0
  234.          Weight          =   700
  235.          Underline       =   0   'False
  236.          Italic          =   0   'False
  237.          Strikethrough   =   0   'False
  238.       EndProperty
  239.       ForeColor       =   &H000000FF&
  240.       Height          =   255
  241.       Index           =   5
  242.       Left            =   4560
  243.       TabIndex        =   9
  244.       Top             =   600
  245.       Width           =   375
  246.    End
  247.    Begin VB.Label Label2 
  248.       BackStyle       =   0  'Transparent
  249.       Caption         =   "8"
  250.       BeginProperty Font 
  251.          Name            =   "Tahoma"
  252.          Size            =   9.75
  253.          Charset         =   0
  254.          Weight          =   700
  255.          Underline       =   0   'False
  256.          Italic          =   0   'False
  257.          Strikethrough   =   0   'False
  258.       EndProperty
  259.       ForeColor       =   &H000000FF&
  260.       Height          =   255
  261.       Index           =   4
  262.       Left            =   3720
  263.       TabIndex        =   8
  264.       Top             =   600
  265.       Width           =   375
  266.    End
  267.    Begin VB.Label Label2 
  268.       BackStyle       =   0  'Transparent
  269.       Caption         =   "16"
  270.       BeginProperty Font 
  271.          Name            =   "Tahoma"
  272.          Size            =   9.75
  273.          Charset         =   0
  274.          Weight          =   700
  275.          Underline       =   0   'False
  276.          Italic          =   0   'False
  277.          Strikethrough   =   0   'False
  278.       EndProperty
  279.       ForeColor       =   &H000000FF&
  280.       Height          =   255
  281.       Index           =   3
  282.       Left            =   2880
  283.       TabIndex        =   7
  284.       Top             =   600
  285.       Width           =   375
  286.    End
  287.    Begin VB.Label Label2 
  288.       BackStyle       =   0  'Transparent
  289.       Caption         =   "32"
  290.       BeginProperty Font 
  291.          Name            =   "Tahoma"
  292.          Size            =   9.75
  293.          Charset         =   0
  294.          Weight          =   700
  295.          Underline       =   0   'False
  296.          Italic          =   0   'False
  297.          Strikethrough   =   0   'False
  298.       EndProperty
  299.       ForeColor       =   &H000000FF&
  300.       Height          =   255
  301.       Index           =   2
  302.       Left            =   2040
  303.       TabIndex        =   6
  304.       Top             =   600
  305.       Width           =   375
  306.    End
  307.    Begin VB.Label Label2 
  308.       BackStyle       =   0  'Transparent
  309.       Caption         =   "64"
  310.       BeginProperty Font 
  311.          Name            =   "Tahoma"
  312.          Size            =   9.75
  313.          Charset         =   0
  314.          Weight          =   700
  315.          Underline       =   0   'False
  316.          Italic          =   0   'False
  317.          Strikethrough   =   0   'False
  318.       EndProperty
  319.       ForeColor       =   &H000000FF&
  320.       Height          =   255
  321.       Index           =   1
  322.       Left            =   1200
  323.       TabIndex        =   5
  324.       Top             =   600
  325.       Width           =   375
  326.    End
  327.    Begin VB.Label Label2 
  328.       BackStyle       =   0  'Transparent
  329.       Caption         =   "128"
  330.       BeginProperty Font 
  331.          Name            =   "Tahoma"
  332.          Size            =   9.75
  333.          Charset         =   0
  334.          Weight          =   700
  335.          Underline       =   0   'False
  336.          Italic          =   0   'False
  337.          Strikethrough   =   0   'False
  338.       EndProperty
  339.       ForeColor       =   &H000000FF&
  340.       Height          =   255
  341.       Index           =   0
  342.       Left            =   360
  343.       TabIndex        =   4
  344.       Top             =   600
  345.       Width           =   375
  346.    End
  347.    Begin VB.Label Label1 
  348.       BackStyle       =   0  'Transparent
  349.       Caption         =   "Enter Decimal Number"
  350.       BeginProperty Font 
  351.          Name            =   "Tahoma"
  352.          Size            =   9.75
  353.          Charset         =   0
  354.          Weight          =   700
  355.          Underline       =   0   'False
  356.          Italic          =   0   'False
  357.          Strikethrough   =   0   'False
  358.       EndProperty
  359.       Height          =   255
  360.       Left            =   360
  361.       TabIndex        =   2
  362.       Top             =   2760
  363.       Width           =   2175
  364.    End
  365.    Begin VB.Menu mnuHelp 
  366.       Caption         =   "&Help"
  367.       Begin VB.Menu mnuTopics 
  368.          Caption         =   "Topics"
  369.       End
  370.    End
  371.    Begin VB.Menu mnuAbout 
  372.       Caption         =   "&About"
  373.       Begin VB.Menu mnuAboutNum 
  374.          Caption         =   "About Number Convertor"
  375.       End
  376.    End
  377. Attribute VB_Name = "frmDecBin"
  378. Attribute VB_GlobalNameSpace = False
  379. Attribute VB_Creatable = False
  380. Attribute VB_PredeclaredId = True
  381. Attribute VB_Exposed = False
  382. Const light_off As String = ("c:\program files\devstudio\vb\graphics\icons\misc\lightoff.ico")
  383. Const light_on As String = ("c:\program files\devstudio\vb\graphics\icons\misc\lighton.ico")
  384. Private Sub Command1_Click()
  385. End Sub
  386. Private Sub Command2_Click()
  387. 'convert dec to bin
  388. Dim bit, dec, num As Single
  389. If txtDec.Text = "" Then
  390.     txtDec.SetFocus
  391.     Exit Sub
  392. End If
  393. dec = Val(txtDec.Text)
  394. txtnum.Text = dec
  395. txtDec.Text = ""
  396. lblout.Caption = "The above is the binary equivilant of the decimal number " & dec
  397. txtDec.SetFocus
  398. 'if number enter is higher than 255 then prompt for new number
  399. If dec > 255 Then
  400.     MsgBox ("Enter a decimal number between 1 and 255 only !"), vbOKOnly
  401.     txtDec.Text = ""
  402.     txtDec.SetFocus
  403.     Exit Sub
  404. End If
  405. 'reset lightbulbs to off
  406. For bit = 0 To 7
  407. bitty(bit).Picture = LoadPicture("C:\program files\devstudio\vb\graphics\icons\misc\lightoff.ico")
  408. Next bit
  409. num = dec
  410. 'if number entered is one then light bit0 bulb and exit
  411. If num = 1 Then
  412.    bitty(0).Picture = LoadPicture("C:\program files\devstudio\vb\graphics\icons\misc\lighton.ico")
  413. bitty(0).Tag = "On"
  414. Exit Sub '
  415. End If
  416. 'create loop to use with arrays
  417. For bit = 0 To 7
  418. 'divide number by 2
  419. num = num / 2
  420. 'if the nearest whole number/2 is more or less than the number/2 then turn that bit on
  421. If Int(num) <> num Then
  422.     bitty(bit).Picture = LoadPicture("C:\program files\devstudio\vb\graphics\icons\misc\lighton.ico")
  423.     bitty(bit).Tag = "On"
  424. End If
  425. 'check for even/odd numbers and adjust to make note of carry
  426. If Int(num) < num Then
  427.     num = Int(num)
  428. ElseIf Int(num) > num Then
  429.     num = Int(num) - 1
  430. End If
  431. 'if num/2 is 1 then light next bit along
  432. If num = 1 Then
  433.    bit = bit + 1
  434.    bitty(bit).Picture = LoadPicture("C:\program files\devstudio\vb\graphics\icons\misc\lighton.ico")
  435.    bitty(bit).Tag = "On"
  436. Exit Sub '
  437. End If
  438. Next bit
  439. Call hexconvert(num)
  440. End Sub
  441. Private Sub bitty_Click(Index As Integer)
  442.   'convert bin to dec
  443.   Dim hex1, hex2 As Variant
  444.   hex1 = Val(txthex1.Text)
  445.   hex2 = Val(txthex2.Text)
  446.   num = Val(txtnum.Text)
  447.   If bitty(Index).Tag = "Off" Then
  448.     bitty(Index).Picture = LoadPicture(light_on)
  449.     bitty(Index).Tag = "On"
  450.    If Index = 0 Then
  451.         num = num + 1
  452.         hex1 = hex1 + 1
  453.    ElseIf Index = 1 Then
  454.         num = num + 2
  455.         hex1 = hex1 + 2
  456.    ElseIf Index = 2 Then
  457.         num = num + 4
  458.         hex1 = hex1 + 4
  459.    ElseIf Index = 3 Then
  460.         num = num + 8
  461.         hex1 = hex1 + 8
  462.    ElseIf Index = 4 Then
  463.         num = num + 16
  464.         hex2 = hex2 + 1
  465.    ElseIf Index = 5 Then
  466.         num = num + 32
  467.         hex2 = hex2 + 2
  468.    ElseIf Index = 6 Then
  469.         num = num + 64
  470.         hex2 = hex2 + 4
  471.    ElseIf Index = 7 Then
  472.         num = num + 128
  473.         hex2 = hex2 + 8
  474.   End If
  475.   txtDec.SetFocus
  476.     bitty(Index).Picture = LoadPicture(light_off)
  477.     bitty(Index).Tag = "Off"
  478.    If Index = 0 Then
  479.         num = num - 1
  480.         hex1 = hex1 - 1
  481.    ElseIf Index = 1 Then
  482.         num = num - 2
  483.         hex1 = hex1 - 2
  484.    ElseIf Index = 2 Then
  485.         num = num - 4
  486.         hex1 = hex1 - 4
  487.    ElseIf Index = 3 Then
  488.         num = num - 8
  489.         hex1 = hex1 - 8
  490.    ElseIf Index = 4 Then
  491.         num = num - 16
  492.         hex2 = hex2 - 1
  493.    ElseIf Index = 5 Then
  494.         num = num - 32
  495.         hex2 = hex2 - 2
  496.    ElseIf Index = 6 Then
  497.         num = num - 64
  498.         hex2 = hex2 - 4
  499.    ElseIf Index = 7 Then
  500.         num = num - 128
  501.         hex2 = hex2 - 8
  502.  End If
  503. End If
  504. txtnum.Text = num
  505. txthex1.Text = hex1
  506. txthex2.Text = hex2
  507. txtDec.SetFocus
  508. Call hexconvert(num)
  509. End Sub
  510. Sub hexconvert(ByVal num As Single)
  511. ' convert to hex
  512. Dim hex1, hex2 As Variant
  513. hex1 = Val(txthex1.Text)
  514. hex2 = Val(txthex2.Text)
  515. If hex1 > 9 Then
  516.     If hex1 = 10 Then
  517.     hex1 = "a"
  518.     ElseIf hex1 = 11 Then
  519.     hex1 = "b"
  520.     ElseIf hex1 = 12 Then
  521.     hex1 = "c"
  522.     ElseIf hex1 = 13 Then
  523.     hex1 = "d"
  524.     ElseIf hex1 = 14 Then
  525.     hex1 = "e"
  526.     ElseIf hex1 = 15 Then
  527.     hex1 = "f"
  528.     Else
  529.     End If
  530. End If
  531. If hex2 > 9 Then
  532.     If hex2 = 10 Then
  533.     hex2 = "a"
  534.     ElseIf hex2 = 11 Then
  535.     hex2 = "b"
  536.     ElseIf hex2 = 12 Then
  537.     hex2 = "c"
  538.     ElseIf hex2 = 13 Then
  539.     hex2 = "d"
  540.     ElseIf hex2 = 14 Then
  541.     hex2 = "e"
  542.     ElseIf hex2 = 15 Then
  543.     hex2 = "f"
  544.     Else
  545.     End If
  546. End If
  547. lblout.Caption = ("The above is the binary equivilant of the decimal number ") & num
  548. lblhex.Caption = ("The Hex equivalent is ") & UCase(hex2) & UCase(hex1)
  549. End Sub
  550. Private Sub Form_Load()
  551. Dim counter
  552. For counter = 0 To 7
  553.   bitty(counter).Picture = LoadPicture(light_off)
  554.   bitty(counter).Tag = "Off"
  555. Next counter
  556. Dim num As Single
  557. frmDecBin.Show
  558. txtDec.SetFocus
  559. End Sub
  560. Private Sub mnuAboutNum_Click()
  561. Dim Msg, Style, Title
  562. Msg = "Coding by Graham Pollitt 15/6/99"
  563. Style = vbOKOnly
  564. Title = "Number Conversion Utility"
  565. Response = MsgBox(Msg, Style, Title)
  566. End Sub
  567.